<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\common\model\ding\OrderSeat as OrderSeatModel;
use cores\exception\BaseException;
use think\facade\Db;

/**
 * Class Article
 * @package app\api\model
 */
class OrderSeat extends OrderSeatModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',

		'update_time',
	];

	public function getWeatherList($shopId) {
		$time = time();
		$list = [];

		$total = Db::name('ding_seat')->where('status', 0)->where('shop_id', $shopId)->count();
		for ($i = 0; $i < 7; $i++) {
			$date=Date('Y-m-d', $time);
			$data['date'] = Date('m-d', $time);
			$data['date2'] = Date('m月d日', $time);

			$data['week'] = get_week_day($time);
			$count = Db::name('ding_order_seat')
				->where('date', $date)
				->where('status', '<>', 30)
				->where('shop_id', $shopId)->count();
			$data['percent'] = $total > 0 ? ($count / $total) * 100 : 0;
			$data['count'] = $count;
			$data['total'] = $total;

			$list[] = $data;

			$time += 86400;
		}

		return $list;
	}

	public function getOrderData(int $shopId = 0, $date = 0) {
		if ($date == 0) {
			$time = strtotime(Date('Y-m-d'));
			//今日数据
			$data['order_count'] = $this->where('status', '<>', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $time)->count();
			//取消订单数
			$data['cancel_count'] = $this->where('status', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $time)->count();
			//门店曝光量
			$data['visit_count'] = Db::name('ding_visit_log')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $time)->count();
			//裂变顾客量
			$data['liebian_count'] = Db::name('ding_shop_visit')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $time)->count();

			//昨日
			$time2 = strtotime(Date('Y-m-d')) - 86400;
			$order_count = $this->where('status', '<>', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $time2)
				->where('create_time', '<', $time)
				->count();
			//取消订单数
			$cancel_count = $this->where('status', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $time2)
				->where('create_time', '<', $time)->count();
			//门店曝光量
			$visit_count = Db::name('ding_visit_log')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $time2)
				->where('create_time', '<', $time)
				->count();
			//裂变顾客量
			$liebian_count = Db::name('ding_shop_visit')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $time2)
				->where('create_time', '<', $time)
				->count();

			$data['order_count_dif'] = $data['order_count'] - $order_count;
			$data['cancel_count_dif'] = $data['cancel_count'] - $cancel_count;
			$data['visit_count_dif'] = $data['visit_count'] - $visit_count;
			$data['liebian_count_dif'] = $data['liebian_count'] - $liebian_count;

			$data['order_count_dif'] = $data['order_count_dif'] > 0 ? '+' . $data['order_count_dif'] : $data['order_count_dif'];

			$data['cancel_count_dif'] = $data['cancel_count_dif'] > 0 ? '+' . $data['cancel_count_dif'] : $data['cancel_count_dif'];
			$data['visit_count_dif'] = $data['visit_count_dif'] > 0 ? '+' . $data['visit_count_dif'] : $data['visit_count_dif'];
			$data['liebian_count_dif'] = $data['liebian_count_dif'] > 0 ? '+' . $data['liebian_count_dif'] : $data['liebian_count_dif'];
		}

		if ($date == 1) {
			$time = strtotime(Date('Y-m-d'));
			$oneWeekAgo = strtotime('-1 week', strtotime(Date('Y-m-d')));
			$lastWeekAgo = strtotime('-1 week', strtotime(Date('Y-m-d'))) - 86400 * 7;

			//7日数据
			$data['order_count'] = $this->where('status', '<>', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $oneWeekAgo)->count();
			//取消订单数
			$data['cancel_count'] = $this->where('status', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $oneWeekAgo)->count();
			//门店曝光量
			$data['visit_count'] = Db::name('ding_visit_log')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $oneWeekAgo)->count();

			//裂变顾客量
			$data['liebian_count'] = Db::name('ding_shop_visit')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $oneWeekAgo)->count();

			//上周

			$order_count = $this->where('status', '<>', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $lastWeekAgo)
				->where('create_time', '<', $oneWeekAgo)
				->count();
			//取消订单数
			$cancel_count = $this->where('status', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $lastWeekAgo)
				->where('create_time', '<', $oneWeekAgo)->count();
			//门店曝光量
			$visit_count = Db::name('ding_visit_log')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $lastWeekAgo)
				->where('create_time', '<', $oneWeekAgo)
				->count();
			//裂变顾客量
			$liebian_count = Db::name('ding_shop_visit')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $lastWeekAgo)
				->where('create_time', '<', $oneWeekAgo)
				->count();

			$data['order_count_dif'] = $data['order_count'] - $order_count;
			$data['cancel_count_dif'] = $data['cancel_count'] - $cancel_count;
			$data['visit_count_dif'] = $data['visit_count'] - $visit_count;
			$data['liebian_count_dif'] = $data['liebian_count'] - $liebian_count;

			$data['order_count_dif'] = $data['order_count_dif'] > 0 ? '+' . $data['order_count_dif'] : $data['order_count_dif'];

			$data['cancel_count_dif'] = $data['cancel_count_dif'] > 0 ? '+' . $data['cancel_count_dif'] : $data['cancel_count_dif'];
			$data['visit_count_dif'] = $data['visit_count_dif'] > 0 ? '+' . $data['visit_count_dif'] : $data['visit_count_dif'];
			$data['liebian_count_dif'] = $data['liebian_count_dif'] > 0 ? '+' . $data['liebian_count_dif'] : $data['liebian_count_dif'];
		}

		if ($date == 2) {
			$time = strtotime(Date('Y-m-d'));
			$oneWeekAgo = strtotime('-1 month', strtotime(Date('Y-m-d')));

			$lastWeekAgo = strtotime('-2 month', strtotime(Date('Y-m-d')));

			//一个月数据
			$data['order_count'] = $this->where('status', '<>', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $oneWeekAgo)->count();
			//取消订单数
			$data['cancel_count'] = $this->where('status', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $oneWeekAgo)->count();
			//门店曝光量
			$data['visit_count'] = Db::name('ding_visit_log')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $oneWeekAgo)->count();

			//裂变顾客量
			$data['liebian_count'] = Db::name('ding_shop_visit')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $oneWeekAgo)->count();

			//上周

			$order_count = $this->where('status', '<>', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $lastWeekAgo)
				->where('create_time', '<', $oneWeekAgo)
				->count();
			//取消订单数
			$cancel_count = $this->where('status', 30)
				->where('shop_id', $shopId)
				->where('create_time', '>=', $lastWeekAgo)
				->where('create_time', '<', $oneWeekAgo)->count();
			//门店曝光量
			$visit_count = Db::name('ding_visit_log')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $lastWeekAgo)
				->where('create_time', '<', $oneWeekAgo)
				->count();
			//裂变顾客量
			$liebian_count = Db::name('ding_shop_visit')
				->where('shop_id', $shopId)
				->where('create_time', '>=', $lastWeekAgo)
				->where('create_time', '<', $oneWeekAgo)
				->count();

			$data['order_count_dif'] = $data['order_count'] - $order_count;
			$data['cancel_count_dif'] = $data['cancel_count'] - $cancel_count;
			$data['visit_count_dif'] = $data['visit_count'] - $visit_count;
			$data['liebian_count_dif'] = $data['liebian_count'] - $liebian_count;

			$data['order_count_dif'] = $data['order_count_dif'] > 0 ? '+' . $data['order_count_dif'] : $data['order_count_dif'];

			$data['cancel_count_dif'] = $data['cancel_count_dif'] > 0 ? '+' . $data['cancel_count_dif'] : $data['cancel_count_dif'];
			$data['visit_count_dif'] = $data['visit_count_dif'] > 0 ? '+' . $data['visit_count_dif'] : $data['visit_count_dif'];
			$data['liebian_count_dif'] = $data['liebian_count_dif'] > 0 ? '+' . $data['liebian_count_dif'] : $data['liebian_count_dif'];
		}

		return $data;
	}

	/**
	 *
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getRealTimeList(int $shopId = 0) {

		for ($type = 1; $type < 3; $type++) {
			$total = Db::name('ding_seat')->where('status', '<>',50)->where('shop_id', $shopId)->count();

			$one = $this->where('type', $type)->where('status', 10)
				->where('yuyue_time', '>=', time())->where('shop_id', $shopId)
				->where('date', Date('Y-m-d'))->count();
			//已到点
			$two = $this->where('type', $type)->where('status', 10)
				->where('shop_id', $shopId)
				->where('yuyue_time', '<', time())->where('date', Date('Y-m-d'))->count();

			$three = 0;
			$four = $this->where('type', $type)->where('status', 20)->where('shop_id', $shopId)
				->where('date', Date('Y-m-d'))->count();
			$five = 0;
			$total = $total == 0 ? 1 : $total;

			$six = $total - $one - $two - $three - $four - $five;
			$six = $six < 0 ? 0 : $six;

			$data[1] = ['name' => '已预订', 'value' => 'one', 'num' => $one, 'percent' => $one / $total * 100];
			$data[2] = ['name' => '已到点', 'value' => 'two', 'num' => $two, 'percent' => $two / $total * 100];
			$data[3] = ['name' => '已确认', 'value' => 'three', 'num' => $three, 'percent' => $three / $total * 100];
			$data[4] = ['name' => '客已到', 'value' => 'four', 'num' => $four, 'percent' => $four / $total * 100];
			$data[5] = ['name' => '已占座', 'value' => 'five', 'num' => $five, 'percent' => $five / $total * 100];
			$data[6] = ['name' => '空闲', 'value' => 'six', 'num' => $six, 'percent' => $six / $total * 100];

			$list[$type] = $data;
		}

		// 获取列表数据
		return $list;
	}

	/**
	 * 获取文章详情并累计阅读次数
	 * @param int $articleId 文章ID
	 * @return static|null
	 * @throws BaseException
	 */
	public static function getDetail($where) {
		// 获取文章详情
		$detail = parent::detail($where, ['order', 'clerk', 'letters']);
		if (empty($detail) || $detail['is_delete']) {
			throwError('很抱歉，当前预订不存在');
		}

		return $detail;
	}

	public function cancel() {

		$this->save(['status' => 30]);
		return true;
	}
	//确认客到
	public function queu() {

		$this->save(['status' => 20]);
		return true;
	}
	public function edit($data2) {
		$data['time'] = $data2['time'];

		$this->save($data);
		return true;
	}

	public static function increased(array $data) {
		Db::name('ding_order_seat')->where('order_id', $data['order_id'])->delete();

		if (isset($data['seatList']) && !empty($data['seatList'])) {
			if (count($data['seatList']) > 1) {
				$is_lian = 1;
			} else {
				$is_lian = 0;
			}
			$newArr = [];
			foreach ($data['seatList'] as $key => $value) {
				$data['seat_id'] = $value['seat_id'];
				$data['is_lian'] = $is_lian;
				$data['name'] = $value['name'];
				$data['store_id'] =  self::$storeId;
				$data['yuyue_time'] = strtotime($data['date'] . ' ' . $data['time']);
				$newArr[] = $data;
			}
			(new static )->saveAll($newArr);

		} else {
			$data['yuyue_time'] = strtotime($data['date'] . ' ' . $data['time']);
			$data['store_id'] =  self::$storeId;
			(new static )->save($data);
		}

		return true;

	}

	public function getListAll($shopId, $param) {
		$params = $this->setQueryDefaultValue($param, [

			'type' => -1,
			'categoryId' => -1,
		]);
		// 检索查询条件
		$filter = [];

		$params['categoryId'] > 0 && $filter[] = ['s.category_id', '=', $params['categoryId']];
		$params['type'] > 0 && $filter[] = ['s.type', '=', $params['type']];
		!empty($params['date']) && $filter[] = ['s.date', '=', $params['date']];
		// 获取列表数据
		return $this->alias('s')
		->join('ding_order o','o.id=s.order_id')
		->with(['clerk', 'letters', 'shop.logoImage', 'order'])
		
			->where($filter)
			->where('o.shop_id', $shopId)
			->where('o.order_status',10)

			->where('s.is_delete', '=', 0)
			->order(['s.create_time' => 'desc'])
			->select()->toArray();
	}
	public function getList($param) {
		$params = $this->setQueryDefaultValue($param, [

			'keyword' => '',
			'type' => -1,
			'categoryId' => -1,
		]);
		// 检索查询条件
		$filter = [];

		$params['categoryId'] > 0 && $filter[] = ['category_id', '=', $params['categoryId']];
		$params['type'] > 0 && $filter[] = ['type', '=', $params['type']];
		!empty($params['keyword']) && $filter[] = ['mobile', '=', $params['keyword']];
		//!empty($params['date']) && $filter[] = ['date', '=', $params['date']];
		// 获取列表数据
		return $this->with(['clerk', 'letters', 'shop.logoImage'])

			->where($filter)

			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->paginate(15);
	}
}